package com.ruoyi.datam.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.base.datam.CmAddvB;
import com.ruoyi.common.core.domain.base.datam.WiOrganB;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.datam.mapper.CmAddvBMapper;
import com.ruoyi.datam.service.ICmAddvBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 行政区划登记Service业务层处理
 *
 * @author hwd
 * @date 2023-02-15
 */
@Service
public class CmAddvBServiceImpl implements ICmAddvBService {
    @Autowired
    private CmAddvBMapper cmAddvBMapper;

    /**
     * 查询行政区划登记
     *
     * @param addvCd 行政区划登记主键
     * @return 行政区划登记
     */
    @Override
    public CmAddvB selectCmAddvBByAddvCd(String addvCd) {
        return cmAddvBMapper.selectCmAddvBByAddvCd(addvCd);
    }

    /**
     * 查询行政区划登记列表
     *
     * @param cmAddvB 行政区划登记
     * @return 行政区划登记
     */
    @Override
    public List<CmAddvB> selectCmAddvBList(CmAddvB cmAddvB) {
        if (cmAddvB.getAddvCd() != null) {
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(cmAddvB.getAddvCd());
            cmAddvB.setAddvCd(addvAndOrgCodeInfo.get("resCode") + "");
        }
        return cmAddvBMapper.selectCmAddvBList(cmAddvB);
    }

    /**
     * 新增行政区划登记
     *
     * @param cmAddvB 行政区划登记
     * @return 结果
     */
    @Override
    public int insertCmAddvB(CmAddvB cmAddvB) {
        //获取
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(cmAddvB.getParentAddvCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        if (grade == 1){
            //查找下属水管机构
            resCode =  resCode + "%0000";
        }else if (grade == 2) {
            //查找下属水管机构
            resCode =resCode + "%00";
        }else if(grade ==3) {
            resCode =resCode+"%";
        }else {
            resCode = resCode;
        }
        cmAddvB.setAddvCd(resCode);
       Integer count= cmAddvBMapper.selectAllCmAddvBList(cmAddvB).size();
        String xh=count.toString();
        if(count<10){
            xh="0"+count;
        }
        if (grade==2){
            cmAddvB.setAddvCd(addvAndOrgCodeInfo.get("resCode")+xh+"00");
        }else if (grade==3){
            cmAddvB.setAddvCd(addvAndOrgCodeInfo.get("resCode")+xh);
        }else {
            return 0;
        }
        return cmAddvBMapper.insertCmAddvB(cmAddvB);
    }

    /**
     * 修改行政区划登记
     *
     * @param cmAddvB 行政区划登记
     * @return 结果
     */
    @Override
    public int updateCmAddvB(CmAddvB cmAddvB) {
        return cmAddvBMapper.updateCmAddvB(cmAddvB);
    }

    /**
     * 批量删除行政区划登记
     *
     * @param addvCds 需要删除的行政区划登记主键
     * @return 结果
     */
    @Override
    public int deleteCmAddvBByAddvCds(String[] addvCds) {
        return cmAddvBMapper.deleteCmAddvBByAddvCds(addvCds);
    }

    /**
     * 删除行政区划登记信息
     *
     * @param addvCd 行政区划登记主键
     * @return 结果
     */
    @Override
    public int deleteCmAddvBByAddvCd(String addvCd) {
        return cmAddvBMapper.deleteCmAddvBByAddvCd(addvCd);
    }

    /**
     * 获取行政区划树形结构
     *
     * @return
     */
    @Override
    public List<CmAddvB> treeList() {
        List<CmAddvB> cmAddvBS = cmAddvBMapper.selectCmAddvBList(null);
        String orgCdQuery;
        for (CmAddvB vo : cmAddvBS) {
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(vo.getAddvCd());
            orgCdQuery = (addvAndOrgCodeInfo.get("resCode") + "").substring(0, (addvAndOrgCodeInfo.get("resCode") + "").length() - 2);
//            CmAddvB cmAddvB = cmAddvBMapper.selectCmAddvBByAddvCd((orgCdQuery + "000000").substring(0, 10));
            String addvCd = (orgCdQuery + "000000").substring(0, 10);
            List<CmAddvB> collect = cmAddvBS.parallelStream().filter(vo2 -> vo2.getAddvCd().equals(addvCd)).collect(Collectors.toList());
            if (collect.size() > 0 && collect.get(0) != null) {
                vo.setParentAddvCd(collect.get(0).getAddvCd());
            }
        }
        return cmAddvBS;
    }
}
